package com.hzp.micromall.common.core.api;

import java.io.Serializable;

/**
 * @Author houzipeng
 * @Date 2022/1/28 22:24
 * @Version 1.0
 */
public class CommonResult<T> implements Serializable {

    private Boolean success;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 相应信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;

    protected CommonResult() {
    }

    protected CommonResult(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 数据
     * @return
     */
    public static <T> CommonResult<T> success(T data){
        return new CommonResult<T>(ResultCode.SUCCESS.success(), ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult<T> success(){
        return new CommonResult<T>(ResultCode.SUCCESS.success(), ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }

    /**
     * 成功返回结果
     *
     * @param data 数据
     * @param message 成功信息
     * @return
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.success(), ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @return
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult<T>(errorCode.success(), errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     *
     * @param message 错误信息
     * @return
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.success(), ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     *
     * @return
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }
//
//    /**
//     * 参数验证失败返回结果
//     *
//     * @return
//     */
//    public static <T> CommonResult<T> validateFailed() {
//        return failed(ResultCode.VALIDATE_FAILED);
//    }
//
//    /**
//     * 参数验证失败返回结果
//     *
//     * @return
//     */
//    public static <T> CommonResult<T> validateFailed(String message) {
//        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
//    }
//
    /**
     * 未登录返回结果
     *
     * @return
     */
    public static <T> CommonResult<T> unLogin(T data) {
        return new CommonResult<T>(ResultCode.TOKEN_INVALID_OR_EXPIRED.success(), ResultCode.TOKEN_INVALID_OR_EXPIRED.getCode(), ResultCode.TOKEN_INVALID_OR_EXPIRED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     *
     * @return
     */
    public static <T> CommonResult<T> unAuthorized(T data) {
        return new CommonResult<T>(ResultCode.ACCESS_UNAUTHORIZED.success(), ResultCode.ACCESS_UNAUTHORIZED.getCode(), ResultCode.ACCESS_UNAUTHORIZED.getMessage(), data);
    }
}
